/*! \file TService.h */
#ifndef __CSERVICE_H__
#define __CSERVICE_H__

#include <windows.h>
#include <string>


/*! Windows NT+ Service framework wrapper class */
class TService {
 public:
   /*! What kind of log level we are sending. */
   enum evLogType  {
	  evError   = EVENTLOG_ERROR_TYPE,
	  evWarning = EVENTLOG_WARNING_TYPE,
	  evInfo    = EVENTLOG_INFORMATION_TYPE
	  };

   /*! Logs the event in the system log. */
   //void LogEvent       (char* e,evLogType t = evInfo,WORD cat = 0);
   void LogEvent		( const std::string & e,evLogType t = evInfo,WORD cat = 0);
 protected:
   TService();
  ~TService();

   /*! Execute */
   bool          Execute                 (void);
   /*! Console Mode */
   bool          ConsoleMode             (void);
   /*! Start the service*/
   bool          Start                   (void);
   /*! Stops the service */
   bool          Stop                    (void);
   /*! Installs the Service */
   bool          Install                 (void);
   /*! Uninstalls the service */
   bool          Remove                  (void);
   /*! Brings up the help dialog */
   virtual bool  Help                    (DWORD context = 0);
   /*! Returns true if the service should be terminated */
   bool          Terminated              (void) {return m_Terminated;}
   /*! Sets Terminated to true */
   virtual void  Terminate               (void) ;

   /*! Returns the last error. */
   const char   *LastError               (void) const  {return m_ErrorString;}
   /*! Prints the last error to the console. */
   void          PrintLastError          (const char *Caption = NULL);
   /*! Prints the text cap to the console. */   
   void			 Print					 ( const std::string & cap );



   /*! Returns the name of the service */
   virtual const char*    GetName        (void) = 0;
   /*! This is the function that does the actual work. */
   virtual void           ServiceProc    (void) = 0;

   /*! Returns the name */
   virtual const char*    GetDisplayName (void)         {return GetName();}
   /*! Initialize the class */
   virtual bool           Init           (void)         {return true;}
   /*! Cleans up. */
   virtual void			  Cleanup        (void)         {return;}
   /*! A user is logged off. (probably doens't fire currently */
   virtual void           LogoffEvent    (void)         {return;}
   /*! The system is being shutdown. */
   virtual void           ShutdownEvent  (void)         {Terminate();}

  private:

   typedef SC_HANDLE tSvcHandle;

   SERVICE_STATUS_HANDLE  m_StatusHandle;
   DWORD                  m_StatusCode;
   HANDLE                 m_EventLog;
   bool                   m_Terminated;
   char                   m_ErrorString[512];

   bool                   shopen         (tSvcHandle &hService);
   void                   shclose        (tSvcHandle hService);
   bool                   succeeded      (BOOL	ReturnValue);

   void                   SetStatus      (DWORD status);
   static void  WINAPI    ServiceMain    (DWORD argc,char* argv[]);
   static void  WINAPI    ServiceHandler (DWORD ServiceControl);
   static BOOL  WINAPI    ConsoleHandler (DWORD dwCtrlType);
};
#endif
